layui.use(['form','treeSelect','layer','upload','laydate'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;
    var treeSelect = layui.treeSelect;

    //上级场景
    treeSelect.render({
        elem: '#tree-select-scenery',
        data: '/scene/tree/select',
        type: 'get',
        placeholder: '请选择地区',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false
        click: function(data){
            $("#tree-select-scenery").val(data.current.id);
        },
        success: function (item) { // 加载完成后的回调函数 areaId
            $("#tree-select-scenery").val("0");
        }
    });

    //所属地区
    treeSelect.render({
        elem: '#tree-select',
        data: '/area/tree/select',
        type: 'get',
        placeholder: '请选择地区',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false
        click: function(data){
            $("#tree-select").val(data.current.id);
            $("#longitude").val(data.current.longitude);
            $("#latitude").val(data.current.latitude);
        },
        success: function (item) { // 加载完成后的回调函数 areaId
            $("#tree-select").val("0");
        }
    });
    //场景类型
    treeSelect.render({
        elem: '#tree-select-category',
        data: '/scene/type/tree/select',
        type: 'get',
        placeholder: '顶级分类',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false
        click: function(data){
            $("#tree-select-category").val(data.current.id);
        },
        success: function (item) { // 加载完成后的回调函数
            $("#tree-select-category").val("0");
        }
    });

    //图片上传 field: 'uploadFile',//上传文件的名称，要和接口参数名称相同

    upload.render({
        elem: '#uploadImg',
        url: '/upload/pic',
        field: "uploadFile",
        before: function(obj){
        },
        done: function(res){
            //上传完毕
            var url = res.url;
            $('#showImg').append('<img style="padding: 10px" width="50%" src="'+ url +'" class="layui-upload-img">');
            $("#imgUrl").val(url);
        }
    });


    //添加场景信息
    form.on('submit(add)', function(data){
        console.log(data);
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            url: '/scene/add',
            data:data.field,
            type:'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("添加失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("添加失败！");
            }
        });
        return false;
    });

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});